﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using DCT.Model;

namespace DCT.ViewModel
{
    public enum SwitchViewModelType
    {
        Merge,
        Normal,
        Reverse,
        Center
    }

    public class SwitchViewModel : DeviceBaseViewModel
    {
        #region Constructor

        public SwitchViewModel(SwitchModel swModel, SwitchGraphModel swGraphModel, SwitchViewModelType swType)
            : base(swModel, swGraphModel, new Point(0.5, 0.5))
        {
            if (swModel == null)
                throw new ArgumentNullException("SwitchModel");

            if (swGraphModel == null)
                throw new ArgumentNullException("SwitchGraphModel");

            _swModel = swModel;
            _swGraphModel = swGraphModel;
            _swViewModelType = swType;

            if (swType == SwitchViewModelType.Merge)
            {
                _mergeSwViewModel = this;
            }
            else if (swType == SwitchViewModelType.Normal)
            {
                _normalSwViewModel = this;
            }
            else
            {
                _reverseSwViewModel = this;
            }
        }

        #endregion // Constructor

        #region Fields

        SwitchModel _swModel;
        SwitchGraphModel _swGraphModel;
        SwitchViewModelType _swViewModelType;
        SwitchViewModel _mergeSwViewModel;
        SwitchViewModel _normalSwViewModel;
        SwitchViewModel _reverseSwViewModel;

        #endregion // Fields

        #region Delegates

        public delegate void UpdateVisualDelegate();
        public UpdateVisualDelegate UpdateVisual;

        #endregion //Delegates

        #region Properties

        public SwitchModel SwitchModel
        {
            get
            {
                return _swModel;
            }
        }

        public SwitchGraphModel SwitchGraphModel
        {
            get
            {
                return _swGraphModel;
            }
        }

        public SwitchViewModel MergeSwViewModel
        {
            get
            {
                return _mergeSwViewModel;
            }
        }

        public SwitchViewModel NormalSwViewModel
        {
            get
            {
                return _normalSwViewModel;
            }
        }

        public SwitchViewModel ReverseSwViewModel
        {
            get
            {
                return _reverseSwViewModel;
            }
        }

        public SwitchViewModelType SwViewModelType
        {
            get
            {
                return _swViewModelType;
            }
        }

        public override Point PositionPoint
        {
            get
            {
                try
                {
                    if (_swViewModelType == SwitchViewModelType.Merge)
                    {
                        return Point.Parse(_swGraphModel.MergePoint);
                    }
                    else if (_swViewModelType == SwitchViewModelType.Normal)
                    {
                        return Point.Parse(_swGraphModel.NormalPoint);
                    }
                    else
                    {
                        return Point.Parse(_swGraphModel.ReversePoint);
                    }

                }
                catch
                {
                    return new Point();
                }
            }
            set
            {
                if (_swViewModelType == SwitchViewModelType.Merge)
                {
                    if (value.ToString() != _swGraphModel.MergePoint)
                    {
                        this.InsertUnDoRedoForProperty("PositionPoint", value, this.PositionPoint);

                        _swGraphModel.MergePoint = value.ToString();
                        base.OnPropertyChanged("PositionPoint");
                        if (this.UpdateVisual != null)
                        {
                            this.UpdateVisual();
                        }
                    }
                }
                else if (_swViewModelType == SwitchViewModelType.Normal)
                {
                    if (value.ToString() != _swGraphModel.NormalPoint)
                    {
                        this.InsertUnDoRedoForProperty("PositionPoint", value, this.PositionPoint);

                        _swGraphModel.NormalPoint = value.ToString();
                        base.OnPropertyChanged("PositionPoint");
                        if (this.UpdateVisual != null)
                        {
                            this.UpdateVisual();
                        }
                    }
                }
                else
                {
                    if (value.ToString() != _swGraphModel.ReversePoint)
                    {
                        this.InsertUnDoRedoForProperty("PositionPoint", value, this.PositionPoint);

                        _swGraphModel.ReversePoint = value.ToString();
                        base.OnPropertyChanged("PositionPoint");
                        if (this.UpdateVisual != null)
                        {
                            this.UpdateVisual();
                        }
                    }
                }
            }
        }

        public override double DeviceHeight
        {
            get
            {
                return base.DeviceHeight;
            }
        }

        public override double DeviceWidth
        {
            get
            {
                return base.DeviceWidth;
            }
        }

        //public override bool IsSelected
        //{
        //    get
        //    {
        //        return _isSelected;
        //    }
        //    set
        //    {
        //        if (value != _isSelected)
        //        {
        //            _isSelected = value;
        //            this.MergeSwViewModel.IsSelected = value;
        //            this.NormalSwViewModel.IsSelected = value;
        //            this.ReverseSwViewModel.IsSelected = value;
        //            base.OnPropertyChanged("IsSelected");
        //        }
        //    }
        //}

        public override bool IsDisplay
        {
            get
            {
                return this.DeviceMapViewModel.DeviceViewModelCollection.Contains(this);
            }
            set
            {
                if (value != this.IsDisplay)
                {
                    if (value)
                    {
                        this.DeviceMapViewModel.DeviceViewModelCollection.Add(this._mergeSwViewModel);
                        this.DeviceMapViewModel.DeviceViewModelCollection.Add(this._normalSwViewModel);
                        this.DeviceMapViewModel.DeviceViewModelCollection.Add(this._reverseSwViewModel);
                    }
                    else
                    {
                        this.DeviceMapViewModel.DeviceViewModelCollection.Remove(_mergeSwViewModel);
                        this.DeviceMapViewModel.DeviceViewModelCollection.Remove(_normalSwViewModel);
                        this.DeviceMapViewModel.DeviceViewModelCollection.Remove(_reverseSwViewModel);
                    }

                    this._mergeSwViewModel.OnPropertyChanged("IsDisplay");
                    this._normalSwViewModel.OnPropertyChanged("IsDisplay");
                    this._reverseSwViewModel.OnPropertyChanged("IsDisplay");
                }
            }
        }

        #endregion //Properties

        #region Methods

        public void SetOtherSwitchViewModel(SwitchViewModel swViewModel)
        {
            if (swViewModel == null)
            {
                throw new ArgumentNullException("OtherSwitchViewModel");
            }

            if (swViewModel.SwViewModelType == this.SwViewModelType)
            {
                throw new ArgumentException("OtherSwitchViewModel");
            }

            if (swViewModel.SwViewModelType == SwitchViewModelType.Merge)
            {
                this._mergeSwViewModel = swViewModel;
            }
            else if (swViewModel.SwViewModelType == SwitchViewModelType.Normal)
            {
                this._normalSwViewModel = swViewModel;
            }
            else
            {
                this._reverseSwViewModel = swViewModel;
            }
        }

        #endregion // Methods
    }
}